package com.mf.all.controller.auth;


import com.mf.all.entity.dto.DtoPermission;
import com.mf.all.entity.req.publicReq.ReqPublicIds;
import com.mf.all.entity.req.user.ReqUserByRole;
import com.mf.all.groups.GroupsAddInterface;
import com.mf.all.groups.GroupsUpdateInterface;
import com.mf.all.result.Result;
import com.mf.all.service.ServicePermission;
import com.mf.all.service.ServiceUserRole;
import com.mf.all.utils.UtilController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.mf.all.entity.dto.DtoRole;
import com.mf.all.entity.req.user.ReqRole;
import com.mf.all.service.ServiceRole;
import com.mf.all.entity.req.search.user.SearchReqRole;

import java.util.List;

/**
 * 角色表
 *
 * @date 2025-04-02 22:36:49
 */
@RestController
@RequestMapping("role")
@Tag(name = "角色表")
public class ControllerRole {
    @Resource
    private ServiceRole serviceRole;

    @Resource
    private ServiceUserRole serviceUserRole;

    @Resource
    private ServicePermission servicePermission;
    @Operation(summary = "分页列表查询")
    @PostMapping("/page")
    @SaCheckPermission("role:search")
    public Result<Page<DtoRole>> findPage(@RequestBody SearchReqRole searchReqRole) {
        return Result.success(serviceRole.findPageByParams(searchReqRole));
    }

    @Operation(summary = "列表查询")
    @PostMapping("/list")
    @SaCheckPermission("role:search")
    public Result<List<DtoRole>> list(@RequestBody SearchReqRole searchReqRole) {

        return Result.success(serviceRole.findListByParams(searchReqRole));
    }

    @Operation(summary = "保存")
    @PostMapping("/save")
    @SaCheckPermission("role:save")
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqRole reqRole) {
        serviceRole.save(UtilController.setBo(reqRole, DtoRole.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @SaCheckPermission("role:update")
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqRole reqRole) {
        serviceRole.updateById(UtilController.setBo(reqRole, DtoRole.class));
        return Result.success();
    }

    @Operation(summary = "删除")
    @PostMapping("/delete")
    @SaCheckPermission("role:delete")
    public Result<Void> delete(@RequestBody ReqPublicIds<Integer> reqPublicIds) {
        serviceRole.removeByIds(reqPublicIds.getIds());
        return Result.success();
    }
    
    @Operation(summary = "角色关联用户")
    @SaCheckPermission("role:roleRelevanceUserId")
    @PostMapping("roleRelevanceUserId")
    public Result<Void> roleRelevanceUserId(@Validated @RequestBody ReqUserByRole reqUserByRole) {
        serviceUserRole.userRoleRelevance(reqUserByRole);
        return Result.success();
    }

    @Operation(summary = "根据角色id查询权限")
    @SaCheckPermission("role:permissionByRoleId")
    @PostMapping("permissionByRoleId")
    public Result<List<DtoPermission>> permissionByRoleId(@Validated @RequestParam("roleId") Integer roleId) {
        return Result.success(servicePermission.permissionByRoleId(roleId));
    }
    

}
